import router from '@/router'
import store from '@/store'
import { asyncRoutes } from '@/router'
// 导航守卫
router.beforeEach(async(to, from, next) => {
  // 判定跳转页面
  if (store.state.user.token) {
    if (to.path === '/login') next('/')
    else {
      // 有token的时候且用户资料为空时请求用户资料
      if (!store.state.user.userInfo.userId) {
        await store.dispatch('user/userMaterial')
        const newRoute = asyncRoutes.filter(item => store.state.user.userInfo.roles.menus.includes(item.name))
        router.addRoutes([...newRoute, { path: '*', redirect: '/404', hidden: true }])
        next(to.path)
        return
      }
      next()
    }
  } else {
    // 没有token时的情况
    const whiteList = ['/login', '/404']
    if (whiteList.indexOf(to.path) !== -1)next()
    else next('/login')
  }
})

